Xendri.closePopup = function() {
	if (!Xendri.popupVisible) return;
	var lnk = Xendri.Location.getLink();
	lnk.removeTarget('popup');
	Xendri.Location.setAnchor(lnk.toString());
	$('popup-layer').removeClass('top');
	$('popup-layer').addClass('hidden');
	$('popup').innerHTML = '';
	$('popup').removeEvent('submit');
	Xendri.popupVisible = false;
}

Xendri.showPopup = function() {
//	$('popup-layer').setOpacity(0.5);
	Bro.log('Searching title...');
	var ti = $$('#popup h1');
	if (ti[0]) {
		Bro.log('Found: '+ti[0].innerHTML);
		Xendri.setPopupTitle(ti[0].innerHTML);
		ti.dispose();
	} else {
		Bro.log('Not found!');
		//Xendri.setPopupTitle('Undefined');
	}
	if (Xendri.popupVisible) return;
	$('popup-close').removeEvents('click');
	$('popup-save').removeEvents('click');
	$('popup').removeEvents('submit');
	$('popup-close').addEvent('click', Xendri.closePopup);
	$('popup-save').addEvent('click', function(evt){
		var frm = $$('#popup form');
		if (frm[0]) {
			$('popup').fireEvent('submit');
		}
	});
	$('popup').addEvent('submit', function(evt) {
		if (evt) evt.stop();
	});
	$('popup-layer').setOpacity(0);
	$('popup-layer').removeClass('hidden');
	$('popup-layer').addClass('top');
	
	var myFx = new Fx.Tween('popup-layer');
	myFx.start('opacity', '0', '1');
	var myFx = new Fx.Tween('popup-layer');
	myFx.start('top', '-600', '0');
	
	Xendri.popupVisible = true;
}

Xendri.setPopupTitle = function (ti) {
	$('popup-name').innerHTML = (ti);
}

document.addEvent('change', function(){
	var loc = Xendri.Location.getAnchor();
	if (!loc.match(/@popup/)) {
		Xendri.closePopup();
		return;
	}
	if($('popup').innerHTML != '') 
		Xendri.showPopup();
	return true;	
});